package com.bigflexlib.utils
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class AssertAndCall
	{
		private var conditionFunc:Function;
		private var callFunc:Function;
		private var args:Array;
		
		private var timer:Timer;
		
		public function AssertAndCall( conditionFunc:Function, callFunc:Function, args:Array=null, delay:Number=100 )
		{
			this.conditionFunc = conditionFunc;
			this.callFunc = callFunc;
			this.args = args;
			
			timer = new Timer( delay, 0 );
			timer.addEventListener( TimerEvent.TIMER, timer_timer );
			timer.start();
		}
		
		private function timer_timer( event:TimerEvent ):void{
			var yes:Boolean;
			try {
				yes = conditionFunc();
			} catch (e:Error ){
				yes = false;
			}
			if ( !yes )
				return;
			timer.removeEventListener(TimerEvent.TIMER, timer_timer );
			timer.stop();	
			callFunc.apply(null,args);
		}
		
	}
}